<?php
namespace app\admin\controller;


use app\common\controller\Base;
use think\Loader;
use think\Request;
use app\common\model\Solution as SolutionModel;
use app\common\model\SolutionCat as SolutionCatModel;
use app\common\model\Product as ProductModel;


class Solution extends Base
{
    public function index(){

        $solutions = SolutionModel::order("id desc")->paginate(20);

        return view('',['solutions'=>$solutions]);

    }

    public function add(){

        $solutionCats = SolutionCatModel::all();

        return view('',['solutionCats'=>$solutionCats]);
    }

    public function edit(Request $request){

        $id =  $request->param('id',0,'intval');
        if(!$id){
            $this->error("非法操作");
        }

        $solutionCats = SolutionCatModel::all();


        $solution = SolutionModel::find($id);

        return view('',['solution'=>$solution,'solutionCats'=>$solutionCats]);
    }

    public function create(Request $request){
        if(!$request->isPost()){
            $this->error("非法操作");
        }
        $data = $request -> only(['title','cover','desc','seo_title','seo_keywords','seo_description','content','catid']);

        $validate = Loader::validate('SolutionValidate');
        $result = $validate-> scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $solutionModel = new SolutionModel();
        $solutionModel -> save($data);

        $this->success("添加成功");

    }

    public function update(Request $request){
        if(!$request->isPost()){
            $this->error("非法操作");
        }

        $data = $request -> only(['title','cover','desc','seo_title','seo_keywords','seo_description','content','catid']);
        $id = $request -> param('id',0,'intval');
        if(!$id){
            $this->error("非法操作");
        }

        $validate = Loader::validate('SolutionValidate');
        $result = $validate-> scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $solutionModel = SolutionModel::find($id);
        $solutionModel -> save($data);

        $this->success("更新成功");
    }

    public function delete(Request $request){

        if(!$request->isDelete()){
            $this->error("非法操作");
        }
        $id = $request -> param('id');
        SolutionModel::destroy($id);
        $this->success("删除成功");

    }



    public function assoc(Request $request){
        $id = $request->param('id',0,'intval');
        $solutionModel = SolutionModel::find($id);


        if(!$solutionModel){
            $this->error("非法渠道访问");
        }

        $assoc = json_decode($solutionModel->assoc);
        if(!$assoc){
            $assoc = [];
        }

        $assocProducts = ProductModel::whereIn('id',$assoc)->select();

        return view('',['solution'=>$solutionModel,'assocProducts'=>$assocProducts]);
    }


    public function forAssoc(Request $request){

        $productList = ProductModel::order('id desc')->paginate(5);

        return view('',['productList'=>$productList]);
    }


    public function addAssoc(Request $request){
        $id = $request->param('id',0,'intval');
        $assocId = $request->param('assocId',0,'intval');

        $solutionModel = SolutionModel::find($id);
        if(!$solutionModel){
            $this->error("来源渠道非法");
        }
        $assoc  = json_decode($solutionModel->assoc);

        $assocProduct = ProductModel::find($assocId);

        if(!$assoc){
            $assoc = [$assocId];
        }else{
            if(in_array($assocId,$assoc)){
                $this->success("操作成功",null,json_decode($assocProduct));
            }else{
                array_push($assoc,$assocId);
            }
        }

        $solutionModel->assoc = json_encode($assoc);
        $solutionModel->save();

        $this->success("操作成功",null,json_decode($assocProduct));
    }

    public function cancelAssoc(Request $request){
        $id = $request->param('id',0,'intval');
        $assocId = $request->param('assocId',0,'intval');

        $solutionModel = SolutionModel::find($id);
        if(!$solutionModel){
            $this->error("来源渠道非法");
        }
        $assoc  = json_decode($solutionModel->assoc);
        if(!$assoc){
            $this->success("操作成功");
        }else{
            $newAssoc = [];
            foreach($assoc as $val ){
                if($val != $assocId){
                    array_push($newAssoc,$val);
                }
            }
        }
        $solutionModel->assoc = json_encode($newAssoc);
        $solutionModel->save();


        $this->success("操作成功");
    }


}